package jwei.apps.adapters;

import java.util.ArrayList;
import java.util.List;

import jwei.apps.R;
import jwei.apps.objects.Review;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ReviewBaseAdapter extends BaseAdapter {

    /** Remember our context so we can use it when constructing views. */
    private Context mContext;

    private List<Review> mItems = new ArrayList<Review>();

    public ReviewBaseAdapter(Context context, List<Review> items) {
        mContext = context;
        mItems = items;
    }

    public int getCount() {
        return mItems.size();
    }

    public Review getItem(int position) {
        return mItems.get(position);
    }

    /** Use the array index as a unique id. */
    public long getItemId(int position) {
        return position;
    }

    /**
     * @param convertView
     *            The old view to overwrite, if one is passed
     * @returns a the updated review object
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        Review r = mItems.get(position);
        if (convertView == null) {
            final LayoutInflater inflater = LayoutInflater.from(mContext);
            View v = inflater.inflate(R.layout.review_list_entry, parent, false);
            TextView summary = (TextView) v.findViewById(R.id.review_summary);
            TextView date = (TextView) v.findViewById(R.id.review_date);
            RatingBar rb = (RatingBar) v.findViewById(R.id.rating_bar);
            rb.setIsIndicator(true);
            
            summary.setText(r.getSummary());
            date.setText(r.getDate());
            rb.setRating(r.getRating());
            rb.setNumStars(5);
            return v;
        } else {
            TextView summary = (TextView) convertView.findViewById(R.id.review_summary);
            TextView date = (TextView) convertView.findViewById(R.id.review_date);
            RatingBar rb = (RatingBar) convertView.findViewById(R.id.rating_bar);
            rb.setIsIndicator(true);
            
            summary.setText(r.getSummary());
            date.setText(r.getDate());
            rb.setRating(r.getRating());
            rb.setNumStars(5);
            return convertView;
        }
    }
}
